const app = getApp();
const api = require('../../utils/api');
const Page = require('../../utils/pageProxy');

Page({
    loginRequired: true,
    data: {
        storeList: [],
        pageNum: 1,
        pageSize: 10,
        total: 0,
        noContent: false,
        isFinished: false, //加载结束
        isLoading: false, //加载中
    },
    onLoad(options) {
        this.getStoreList();
    },
    onShow() {

    },
    getStoreList() {
        this.setData({
            isLoading: true
        });

        let params = {
            shopType: 1, //店铺类型：0普通店铺 1社区自营 2平台自营
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        }
        api.post(`/index/shop/recommend`, params)
            .then(res => {
                let list = res.content;
                list.forEach(item => {
                    //整理营业时间
                    let businessBeginTime = item.businessBeginTime.split(':');
                    let businessEndTime = item.businessEndTime.split(':');
                    item.businessTime = businessBeginTime[0] + ':' + businessBeginTime[1] + '~' + businessEndTime[0] + ':' + businessEndTime[1];
                });

                let newList = [...this.data.storeList, ...list];
                this.setData({
                    storeList: newList,
                    total: res.total,
                    showSearchResult: true,
                    isLoading: false,
                });

                //是否无内容标识
                if(this.data.storeList.length == 0) {
                    this.setData({
                        noContent: true
                    })
                } else {
                    this.setData({
                        noContent: false
                    });
                }
                //是否加载完毕标识
                if(this.data.storeList.length >= this.data.total) {
                    this.setData({
                        isFinished: true
                    });
                } else {
                    this.setData({
                        isFinished: false
                    });
                }
                wx.stopPullDownRefresh();
            })
    },
    gotoCommunity(e) { //查看社区（部门）
        let item = e.currentTarget.dataset.item;
        wx.setStorageSync('shopId', item.shopId); //保存店铺id
        app.globalData.isRefreshStore = true; //刷新店铺标识
        // wx.switchTab({
        //     url: '/pages/storeIndex/storeIndex',
        // });
        wx.navigateTo({
            url: '/pages/storeIndex/storeIndex',
        });
    },

    onPullDownRefresh() {
        //下拉刷新
        this.setData({
            storeList: [],
            pageNum: 1,
            total: 0,
            isFinished: false,
            isLoading: false,
        });
        this.getStoreList();
    },
    onReachBottom() {
        //加载更多
        if(!this.data.isFinished && !this.data.isLoading) {
            this.setData({
                pageNum: this.data.pageNum+1,
            });
            
            setTimeout(() => {
                this.getStoreList();
            }, 800);
        }
    },
})